package top.abrsm.util;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * @Author: Wuzh
 * @Description: 工具类
 * @Date Created in 13:46 2020/9/10
 * @return
 */
public class CommonUtil {


    // 阿里云时间戳格式
    public static final SimpleDateFormat oss = new SimpleDateFormat ( "yyyy-MM-dd" );

    public static final SimpleDateFormat sdf = new SimpleDateFormat ( "yyyyMMdd" );


    public static String getCurrentSimpleDate () {
        return sdf.format ( Calendar.getInstance ().getTime () );
    }



    public static Date getCurrDate ( int isMin ) {
        SimpleDateFormat sdFormat = null;
        if ( isMin == 1 ) {
            sdFormat = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss SSS" );
        } else {
            sdFormat = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" );
        }
        Date date = new Date ();
        String dateString = sdFormat.format ( date );
        ParsePosition pos = new ParsePosition ( 0 );
        Date currentTime_2 = sdFormat.parse ( dateString, pos );
        return currentTime_2;
    }

    /**
     * 将时间转化成指定格式-Date-String
     *
     * @param date
     * @param zdFormat
     * @return String
     */
    public static String getDateStrFormat ( Date date, String zdFormat ) {
        String reString = "";
        try {
            SimpleDateFormat sdFormat = new SimpleDateFormat ( zdFormat );
            //Date date = sdFormat.parse(orDate);
            reString = sdFormat.format ( date );
        } catch ( Exception e ) {
            // TODO: handle exception
        }
        return reString;
    }


    /**
     * @return
     * @Author: Wuzh
     * @Description: 获取系统当前格式化时间
     */
    public static String getFormatDateTime ( String format ) {
        LocalDateTime localDateTime = LocalDateTime.now ();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern ( format );
        return dateTimeFormatter.format ( localDateTime );
    }


    /**
     * 获取给定长度的随机整数
     *
     * @param length 长度
     * @return
     */
    public static String getInviteCode ( int length ) {
        StringBuilder sb = new StringBuilder ();
        Random random = new Random ();
        char arrays[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
        for ( int i = 0; i < length; i++ ) {
            sb.append ( arrays[random.nextInt ( arrays.length )] );
        }
        return sb.toString ();
    }


    /**
     * 获取给定长度的整数随机数
     *
     * @param length 长度
     * @return
     */
    public static String getRandom ( int length ) {
        StringBuilder sb = new StringBuilder ();
        Random random = new Random ();
        int arrays[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        for ( int i = 0; i < length; i++ ) {
            sb.append ( arrays[random.nextInt ( arrays.length )] );
        }
        return sb.toString ();
    }


    /**
     * 生成token
     *
     * @return
     */
    public static String getToken () {
        return UUID.randomUUID ().toString ();
    }


}
